﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LDCRNewEdit.Utils
{
    class Tools
    {
        /// <summary>
        /// 把字符串的值转为byte[]类型
        /// </summary>
        /// <param name="value">数值</param>
        /// <param name="len">长度，1byte, 2short, 4int</param>
        /// <param name="isStr">是否为字符串，如果为字符串则转为固定len长度</param>
        /// <returns></returns>
        public static byte[] StringToBytes(string value, int len, bool isStr)
        {
            if (string.IsNullOrEmpty(value))
            {
                return null;
            }
            if (isStr)          //如果值是字符串类型
            {
                byte[] buf = new byte[len];

                CharSetUtil.GbkToBig5(value).CopyTo(buf, 0);

                return buf;
            }
            else
            {
                //byte字符串
                if (len == 1)
                {
                    byte b = byte.Parse(value);
                    return new byte[] { b };
                }
                else if (len == 2)      //short字符串
                {
                    return ShortToBytes(short.Parse(value));
                }
                else if (len == 4)      //int字符串
                {
                    return IntToBytes(int.Parse(value));
                }
            }

            return null;
        }

        /// <summary>
        /// 把byte转为字符串
        /// </summary>
        /// <param name="data"></param>
        /// <param name="len"></param>
        /// <param name="isStr"></param>
        /// <param name="offset">偏移位置</param>
        /// <returns></returns>
        public static string BytesToString(byte[] data, int len, bool isStr, int offset)
        {
            if (isStr)
            {
                return CharSetUtil.Big5ToGbkStr(data.Skip(offset).Take(len).ToArray());
            }
            else
            {
                //byte字符串
                if (len == 1)
                {
                    //return (data[offset] == byte.MaxValue ? -1 : data[offset]).ToString();
                    return ((short)(data[offset] | 0x0000)).ToString();

                }
                else if (len == 2)      //short字符串
                {
                    return BytesToShort(data, offset).ToString();
                }
                else if (len == 4)      //int字符串
                {
                    return BytesToInt(data, offset).ToString();
                }
            }

            return string.Empty;
        }

        /// <summary>
        /// 把int转为byte数组
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte[] IntToBytes(int value)
        {
            byte[] result = new byte[4];
            result[3] = (byte)(value >> 24);
            result[2] = (byte)(value >> 16);
            result[1] = (byte)(value >> 8);
            result[0] = (byte)value;
            return result;
        }

        /// <summary>
        /// byte数组转为int
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        public static int BytesToInt(byte[] bytes, int offset)
        {
            return (bytes[offset + 3] & 0xFF << 24) | ((bytes[offset + 2] & 0xFF) << 16) | ((bytes[offset + 1] & 0xFF) << 8) | ((bytes[offset] & 0xFF));
        }

        /// <summary>
        /// short转为byte
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte[] ShortToBytes(short value)
        {

            byte[] result = new byte[2];
            result[1] = (byte)(value >> 8);
            result[0] = (byte)value;

            return result;
        }

        /// <summary>
        /// byte数组转为short
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        public static short BytesToShort(byte[] bytes, int offset)
        {
            short num = (short)(bytes[offset] & 0xFF);
            num |= (short)((bytes[offset + 1] << 8) & 0xFF00);
            return num;
        }


        public static short BytesToShort(byte b1, byte b2)
        {
            short num = (short)(b1 & 0xFF);
            num |= (short)((b2 << 8) & 0xFF00);
            return num;
        }

    }
}
